//Write by oshogbo//vx
//23.07.2011

import java.io.*;
import java.lang.*;

class Channels implements Runnable 
{
  private final static int maxNameLenght = 10;

  private ChannelType channelType;
  private String nameToShow;
  private Thread thread;
  private int timeSleep = 4;
  private boolean endThread = false;
  private boolean wasChanged = false;

  public Channels(String name, ChannelType channelType, int timeSleep)
  {
    this.channelType = channelType;
    nameToShow = name;
    setTime(timeSleep);

    if(nameToShow.length() > maxNameLenght)
      nameToShow = nameToShow.substring(0, maxNameLenght);

    thread = new Thread(this);
    thread.setDaemon(true);
    thread.start();
  }

  public boolean wasChanged()
  {
    boolean ret = false; 
    if(wasChanged == true)
      ret = true;

    wasChanged = false;
    return ret;
  }

  public void run()
  {
    while( !endThread )
    {
      boolean oldStatus = channelType.isLive(); 
      channelType.checkStatus();

      if( channelType.isLive() == true && oldStatus != channelType.isLive() )
	wasChanged = true;

      try
      {
	thread.sleep(timeSleep);
      }catch(Exception e)
      {

      }
    }
  }

  public void destroy()
  {
    endThread = true;
  }

  public void setTime(int timeSleep)
  {
    this.timeSleep = timeSleep;
  }

  public String getNameToShow()
  {
    return nameToShow;
  }

  public void checkStatus()
  {
    channelType.checkStatus();
  }

  public String getName()
  {
    return channelType.getName();
  }

  public String getAddres()
  {
    return channelType.getAddres();
  }

  public boolean isLive()
  {
    return channelType.isLive();
  }
}
